<?php

namespace App\common\Pay;

use App\Admin\Services\InPayService;
use App\Admin\Services\OutPayService;
use App\common\Log;
use App\Models\PayExchange\PayOrderModel;
use App\utility\HttpHelper;

class SafePay extends Base
{
    public function pay($data)
    {

        if ($data['payType'] == 4) {
            $paycode = '21001';
        }
        if ($data['payType'] == 6) {
            $paycode = '21002';
        }
        $username = str_replace(' ', '', $data['username']);
        $params = [
            "paytypecode" => $paycode,
            "payname" => $username,
            'payemail' => $data['user_id'] . '@gmail.com',
            'payphone' => $data['phone'],
            "currency" => "PHP",
            "mer_no" => $this->config['mer_no'],
            "method" => "trade.create",
            "order_amount" => (string)$data['amount'],
            "order_no" => $data['order_no'],
            "returnurl" => $this->config['notify'],
        ];
        $params['sign'] = $this->sign($params);
//        $params['sign'] = $this->sign(["order_amount" => (string)$data['amount'], "order_no" => $data['order_no']]);
        Log::write('safepay支付请求响应：' . json_encode($params), 'pay', 'pay');
//        $url = 'http://gw.gcashs.com/api/pay?' . http_build_query($params);
//        $retJson = HttpHelper::get($url);
        $retJson = HttpHelper::post($this->config['url'], json_encode($params));
        $ret = json_decode($retJson, true);
        Log::write('safepay支付请求响应：' . $retJson, 'pay', 'pay');
//        Log::info('safepay:---支付请求'.$retJson);
        if ($ret && $ret['status'] == 'success') {
            $data['transId'] = $ret['order_no'];
            $res = InPayService::insert($data);
            if ($res) {
                $data['order_data'] = $ret['order_data'];
                $data['success'] = true;
                return $data;
            }
            return $data;
        }
        return $data;

    }

    public function notify(array $params)
    {
        $sign = $params['sign'];
        Log::write('safepay支付回调响应：' . json_encode($params), 'pay', 'pay');
        unset($params['sign'], $params['s']);
        $curSign = $this->sign($params);
        if ($sign != $curSign) {
            $this->err = 'sign err';
            return false;
        }
        $order = $this->orderInfo($params['order_no']);

        if ($order === false) {
            return false;
        }

        if ($params['status'] == 'success') {//成功
            $order->Amount = intval($params['order_realityamount']);
            $order->Remark = $params['status'];
            return InPayService::success($order, $this->err);
        } else {//失败
            $order->Remark = $params['status'];
            return InPayService::error($order, $this->err);
        }

    }

    public function sign($data)
    {
        ksort($data);
        $str = '';
        foreach ($data as $key => $val) {
            if (!$val) continue;
            $str .= $key . '=' . $val . '&';
        }
        $str = substr($str, 0, strlen($str) - 1);
        $str .= $this->config['key'];
        return strtolower(md5($str));
    }

}